package com.dhcc.kafka.receiver;

import java.util.Optional;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import com.dhcc.kafka.entity.Message;
import com.google.gson.Gson;

import lombok.extern.slf4j.Slf4j;

/**
 * 
* <p>标题: KafkaReceiver.java</p>
* <p>业务描述:KafkaReceiver</p>
* <p>公司:东华软件股份公司</p>
* <p>版权:dhcc2013</p>
* @author 胡凯
* @date 2019年7月31日
* @version V1.0
 */
@Component
@Slf4j
public class KafkaReceiver {

	//与发送者相呼应
    @KafkaListener(topics = {"kafka_kill"})
    public void listen(ConsumerRecord<?, ?> record) {
        Optional<?> kafkaMessage = Optional.ofNullable(record.value());
        if (kafkaMessage.isPresent()) {
            Object message = kafkaMessage.get();
            Message me = new Gson().fromJson(message.toString(), Message.class);
            log.info("record =" + record);
            log.info("message =" + me);
            log.info("message =" + message);
        }
        log.info("topic名称:" + record.topic() 
        		+ ",key:" + record.key() 
        		+ ",分区位置:" + record.partition()
        		+ ", 下标" + record.offset());

    }
}